import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:image_picker/image_picker.dart';

class CameraGalleryWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _CameraGalleryWidgetState();
}

class _CameraGalleryWidgetState extends State<CameraGalleryWidget> {
  File _imgPath;
  String logTag = "<<<_CameraGalleryWidgetState";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    print("$logTag initState()");
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    print("$logTag build()");
    return Scaffold(
        appBar: AppBar(
          title: Text("相机相册模块"),
        ),
        body: SingleChildScrollView(
            child: Platform.isAndroid
                ? FutureBuilder<void>(
                    future: retrieveLostData(),
                    builder:
                        (BuildContext context, AsyncSnapshot<void> snapshot) {
                      return _btnWidget();
                    },
                  )
                : _btnWidget()) // This trailing comma makes auto-formatting nicer for build methods.
        );
  }

  Widget _btnWidget() {
    return Container(
      width: double.infinity,
      alignment: Alignment.center,
      child: Column(
        children: <Widget>[
          Padding(
            padding: EdgeInsets.symmetric(vertical: 20.0),
            child: CircleAvatar(
              backgroundImage: _imgPath == null ? AssetImage("assets/images/avatar.jpeg") : FileImage(_imgPath),
              backgroundColor: Color(0x00000000),
              radius: 100.0,
            ),
          ),
          CupertinoButton(
            child: Text('Pick Photo'),
            color: Colors.blue,
            onPressed: _showBottomDialog,
          )
        ],
      ),
    );
  }

  Future _showBottomDialog() {
    return showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return Padding(
            padding: EdgeInsets.symmetric(vertical: 10.0),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  child: Container(
                    width: double.infinity,
                    alignment: Alignment.center,
                    padding: EdgeInsets.symmetric(vertical: 10.0),
                    child: Text('Camera'),
                  ),
                  onTap: _takePhoto,
                ),
                Divider(
                  color: Colors.grey,
                ),
                GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  child: Container(
                    width: double.infinity,
                    alignment: Alignment.center,
                    padding: EdgeInsets.symmetric(vertical: 10.0),
                    child: Text('Album'),
                  ),
                  onTap: _openGallery,
                ),
                Divider(
                  color: Colors.grey,
                ),
                GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  child: Container(
                    width: double.infinity,
                    alignment: Alignment.center,
                    padding: EdgeInsets.symmetric(vertical: 10.0),
                    child: Text('Cancel'),
                  ),
                  onTap: () {
                    Navigator.of(context).pop();
                  },
                )
              ],
            ),
          );
        });
  }

  _takePhoto() async {
    Navigator.of(context).pop();
    try {
      _imgPath = await ImagePicker.pickImage(source: ImageSource.camera);
      setState(() {});
    } catch (e) {
      print(e);
    }
  }

  _openGallery() async {
    Navigator.of(context).pop();
    _imgPath = await ImagePicker.pickImage(source: ImageSource.gallery);
    setState(() {});
  }

  Future<void> retrieveLostData() async {
    final LostDataResponse response = await ImagePicker.retrieveLostData();
    if (response.isEmpty) {
      return;
    }
    if (response.file != null) {
      setState(() {
        _imgPath = response.file;
      });
    }
  }
}
